<template>
  <div class="login">
    <el-card style="max-width: 480px">
      <el-radio-group v-model="title" style="margin-bottom: 20px">
        <el-radio-button label="登录" />
        <el-radio-button label="注册" />
      </el-radio-group>

      <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
        class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone" v-if="title === '注册'">
          <el-input v-model="ruleForm.phone" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="submitForm(ruleFormRef)" v-if="title === '登录'">
          登录
        </el-button>
        <el-button @click="resetForm(ruleFormRef)" v-else type="primary">注册</el-button>
      </div>
    </el-card>
  </div>
</template>
<script lang="js" setup>
// import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'
const router = useRouter()
let title = ref('登录')
const formSize = ref('default')
const ruleFormRef = ref()
const ruleForm = reactive({
  name: '',
  password: '',
  phone: ''
})
const rules = reactive({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入短话号码', trigger: 'blur' },
  ],

})
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      router.push('/')
      console.log('submit!')
      ElMessage({
        message: '登录成功',
        type: 'success',
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
const resetForm = async () => {
  ElMessage({
    message: '注册成功',
    type: 'success',
  })
}

</script>
<style lang="less" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url(../../assets/login/login-bg.jpg);
  background-repeat: no-repeat;
  background-size: cover;

  h3 {
    margin-bottom: 30px;
  }
}
</style>
